# =============================================================================
# FILE: 06_response_latency.R
# DESCRIPTION:
#   Analyzes response latency in the cueing experiment using survival models.
#   Produces supplemental tables for response time analysis.
#
# PACKAGES REQUIRED: pacman, tidyverse, lme4, stargazer, miceadds, survival
#
# OUTPUTS:
#   - 04_outputs/table_a08.doc
# =============================================================================

# Packages and dataset ---------------------------------------------------------
# Required packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, 
               lme4, 
               stargazer, 
               miceadds, 
               survival)
# Data
df_e <- readRDS("04_outputs/clean_dataset_reshaped.rds")

# Variables for models ---------------------------------------------------------

# Ingroup vs Outgroup cue
df_e$inout <- ifelse((df_e$cha_id == "Fujimorismo" &
                        df_e$cond == 'fuji') |
                       (df_e$cha_id == "Anti-Fujimorismo" &
                          df_e$cond == 'anti'), "ingroup", 
                     ifelse((df_e$cha_id == "Fujimorismo" &
                               df_e$cond == 'anti') |
                              (df_e$cha_id == "Anti-Fujimorismo" &
                                 df_e$cond == 'fuji'), "outgroup", 'other'))

df_e$inout <- factor(df_e$inout, levels = c('other', 
                                            "ingroup", 
                                            "outgroup"))

# In and outgroup cues together
df_e$inout2 <- ifelse(df_e$inout == 'other', 
                      'other', 
                      'inout')
df_e$inout2 <- factor(df_e$inout2, levels = c('other', 
                                              'inout'))

# Condition
df_e$cond <- factor(df_e$cond, levels = c("none", 
                                          "fuji", 
                                          'anti'))
# Movement-based identities
df_e$cha_id <- factor(df_e$cha_id, levels = c("none", 
                                              "Fujimorismo",
                                              "Anti-Fujimorismo"))

# EXPERIMENT TYPE
df_e$exp_type <- ifelse(df_e$exp == 1 |
                          df_e$exp == 4 |
                          df_e$exp == 6, "fuji", "anti-fuji")

# Logged times
df_e$ltime_f <- log(df_e$time_f) # First click 
df_e$ltime_l <- log(df_e$time_l) # Last click
df_e$ltime_p <- log(df_e$time_p) # Page click

# Response latency models ------------------------------------------------------

# Models without controls
t0_p <- coxph(Surv(time_p) ~ inout, data = df_e)
t0_f <- coxph(Surv(time_f) ~ inout, data = df_e)
t0_l <- coxph(Surv(time_l) ~ inout,  data = df_e)
t1_p <- coxph(Surv(time_p) ~ inout2, data = df_e)
t1_f <- coxph(Surv(time_f) ~ inout2, data = df_e)
t1_l <- coxph(Surv(time_l) ~ inout2, data = df_e)

# Models with Controls
t2_p <- coxph(Surv(time_p) ~ inout + party_id + pb06 + as.character(order) + exp, data = df_e)
t2_f <- coxph(Surv(time_f) ~ inout  + party_id + pb06 + as.character(order) + exp, data = df_e)
t2_l <- coxph(Surv(time_l) ~ inout  + party_id + pb06 + as.character(order) + exp, data = df_e)
t3_p <- coxph(Surv(time_p) ~ inout2  + party_id + pb06 + as.character(order) + exp, data = df_e)
t3_f <- coxph(Surv(time_f) ~ inout2  + party_id + pb06 + as.character(order) + exp, data = df_e)
t3_l <- coxph(Surv(time_l) ~ inout2  + party_id + pb06 + as.character(order) + exp, data = df_e)

# TABLE
stargazer(
  t0_f,
  t0_l,
  t0_p,
  t1_f,
  t1_l,
  t1_p,
  t2_f, 
  t2_l,
  t2_p,
  t3_f, 
  t3_l,
  t3_p,
  align = TRUE,
  type = "html", 
  out = "04_outputs/table_a08.doc",
  dep.var.labels = c("First Click", 
                     "Second Click", 
                     "Page Click", 
                     "First Click", 
                     "Second Click", 
                     "Page Click", 
                     "First Click", 
                     "Second Click", 
                     "Page Click", 
                     "First Click", 
                     "Second Click", 
                     "Page Click"),
  covariate.labels = c("Ingroup (ref. Other)", 
                       "Outgroup (ref. Other)", 
                       "Ingroup or Outgroup (ref. Other)"),
  omit = c("party_id", "pb06", "order", "exp"),
  omit.stat = c("wald", "lr", "ll", "logrank", "max.rsq"),
  add.lines = list(c("Controls", "No", "No", "No", "No", "No", "No", 
                     "Yes", "Yes", "Yes", "Yes", "Yes", "Yes")),
  table.layout = "=ldm#c-t-sa-n")